//
//  YFIntegralAreaViewController.m
//  QuanJieShopProject
//
//  Created by apple on 2018/5/29.
//  Copyright © 2018年 apple. All rights reserved.
//

#import "YFIntegralAreaViewController.h"
#import "UIScrollView+EmptyDataSet.h"
#import "YFHomeOneCell.h"
#import "QJShopDetailVC.h"
#import "YFHomeZeroReusableView.h"
#import "YFShopRegisterVC.h"
#import "YFIntegralAreaHeaderView.h"
#import "YFWebViewController.h"
#import "SDCycleScrollView.h"
@interface YFIntegralAreaViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout,DZNEmptyDataSetDelegate,DZNEmptyDataSetSource,SDCycleScrollViewDelegate>{
    int _page;
}
@property (nonatomic, strong) UICollectionView *homeCollectionView;
@property (nonatomic, strong) SDCycleScrollView *cycleView;
@property (nonatomic, strong) NSMutableArray *goodsListArray;

@property (nonatomic, strong) NSMutableArray *bannerArray;
@end

@implementation YFIntegralAreaViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //界面布局
    [self setupHomeView];
    self.title = @"积分专区";
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    _page = 1;
    [self dataRequestHomeAction];
}

#pragma mark -- 界面布局
- (void)setupHomeView{
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    
    //滚动方向(默认垂直)
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    self.homeCollectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, UISCREEN_WIDTH, UISCREEN_HEIGHT) collectionViewLayout:layout];
    
    [self.view addSubview:self.homeCollectionView];
    self.homeCollectionView.delegate = self;
    self.homeCollectionView.dataSource = self;
    self.homeCollectionView.emptyDataSetDelegate = self;
    self.homeCollectionView.emptyDataSetSource = self;
    self.homeCollectionView.backgroundColor = YFColor_Background;
    //在cell的个数不多的时候也可以竖直滑动
    self.homeCollectionView.alwaysBounceVertical = YES;
    
    // 不用Xib,用代码实现
    [self.homeCollectionView registerClass:[YFIntegralAreaHeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerOne"];
    
    //cell
    [self.homeCollectionView registerClass:[YFHomeOneCell class] forCellWithReuseIdentifier:@"cellTwo"];
    // 刷新
    self.homeCollectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(headerRefreshNear)];
    self.homeCollectionView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(footerRefreshNear)];
}

#pragma mark -- 数据刷新
- (void)headerRefreshNear{
    _page = 1;
    [self dataRequestHomeAction];
}
- (void)footerRefreshNear{
    _page++;
    [self dataRequestHomeAction];
}
#pragma mark -- 数据请求
- (void)dataRequestHomeAction{
    [self.homeCollectionView.mj_footer endRefreshing];
    [self.homeCollectionView.mj_header endRefreshing];
    
    NSNumber *pageNum = [NSNumber numberWithInt:_page];
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setObject:pageNum forKey:@"p"];
    NSLog(@"_page = %d", _page);
    [WWNetRequest fetchIntegralAreaWithParameters:dict success:^(id response) {
        
        NSLog(@"积分专区response = %@", response);
        NSDictionary *data = response[@"data"];
        
        // 轮播图
        NSArray *bannerArr = data[@"banner"];
        NSMutableArray *tempBannerArr = [NSMutableArray array];
        for (NSDictionary *dict in bannerArr) {
            HbannerModel *model = [[HbannerModel alloc]initWithDictionary:dict error:nil];
            [tempBannerArr addObject:model];
        }
        // self.bannerArray

        // 商品列表
        NSArray *goodListArr = data[@"goods_list"];
        NSMutableArray *tempGoodListArr = [NSMutableArray array];
        for (NSDictionary *dict in goodListArr) {
            // 这里先用SearchMdoel ,后期再改名字
            SearchMdoel *model = [[SearchMdoel alloc]initWithDictionary:dict error:nil];
            [tempGoodListArr addObject:model];
        }
        // self.goodsListArray
        
        if (_page == 1) {
            self.bannerArray = tempBannerArr;
            self.goodsListArray = tempGoodListArr;
        }else {
            for (HbannerModel *model in tempBannerArr) {
                [self.bannerArray addObject:model];
            }
            
            for (SearchMdoel *model in tempGoodListArr) {
                [self.goodsListArray addObject:model];
            }
        }
        
        //刷新数据
        [self.homeCollectionView reloadData];
        
    } failure:^(NSDictionary *error) {
        
    }];
}

#pragma mark - UICollectionViewDataSource -
#pragma mark -- 返回section数量
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}

#pragma mark -- 返回item数量
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.goodsListArray.count;
}

#pragma mark -- 重用item
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    YFHomeOneCell *oneCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellTwo" forIndexPath:indexPath];
    [oneCell cellUpdateWithModel:self.goodsListArray[indexPath.row]];
    return oneCell;
}
#pragma mark -- 区头
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    WEAKSELF
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {

        UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerOne" forIndexPath:indexPath];
        SDCycleScrollView *cycleScrollView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, UISCREEN_WIDTH, UISCREEN_HEIGHT * 420.f/1334.f) delegate:self placeholderImage:[UIImage imageNamed:@"HBanner"]];
        cycleScrollView.pageControlAliment = SDCycleScrollViewPageContolAlimentCenter;
        cycleScrollView.currentPageDotImage = [UIImage imageNamed:@"banner_dian"];
        cycleScrollView.pageDotImage = [UIImage imageNamed:@"banner_kong"];
        [headerView addSubview:cycleScrollView];
        self.cycleView = cycleScrollView;
//        headerView.clickItemOperationBlock = ^(NSInteger currentIndex) { // 轮播图
//            //                    NSLog(@"currentIndex = %d", currentIndex);
//            HbannerModel *model = _bannerArray[currentIndex];
//            if ([model.status intValue] == 0) { // 0:不跳转
//            }else if ([model.status intValue] == 1) {  // 1:商品页
//
//                QJShopDetailVC *VC = [QJShopDetailVC new];
//                VC.goodsID = [NSString stringWithoutNilFormart:model.goods_id];
//                KPushViewControllerAction(VC);
//
//            }else if ([model.status intValue] == 2) {  // 2:商家入驻
//                YFShopRegisterVC *registerVC = [[YFShopRegisterVC alloc] init];
//                KPushViewControllerAction(registerVC);
//
//            }else if ([model.status intValue] == 3) {  // 3:跳转linkurl
//                NSString *url = [NSString stringWithFormat:@"%@%@",NET_BASEURL,model.ad_link];
//
//                YFWebViewController *vc = [[YFWebViewController alloc] init];
//                vc.urlString = url;
//                KPushViewControllerAction(vc);
//            }
//        };
//        headerView.bannerArray = self.bannerArray;
        return headerView;
    }else{
        //区尾
        return nil;
    }
}
#pragma mark -- UICollectionViewDelegate
#pragma mark -- 点击事件
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    QJShopDetailVC *VC = [QJShopDetailVC new];
    SearchMdoel *model = self.goodsListArray[indexPath.row];
    VC.goodsID = [NSString stringWithoutNilFormart:model.goods_id];
    KPushViewControllerAction(VC);
}
#pragma mark - UICollectionViewDelegateFlowLayout -
#pragma mark -- 定义每个item的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    CGFloat padding = 3;
    CGFloat w = (UISCREEN_WIDTH - padding*2)/2;
    CGFloat h = w * 520.f/370.f;
    return CGSizeMake(w, h);
}
#pragma mark -- item的缩进量
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    return UIEdgeInsetsMake(0, 0, 0, 0);
}
#pragma mark -- 设置单元格的横向间距
- (CGFloat)collectionView:(UICollectionView *)collectionView
                   layout:(UICollectionViewLayout *)collectionViewLayout
minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 0;
}
#pragma mark -- 设置单元格的纵向间距
- (CGFloat)collectionView:(UICollectionView *)collectionView
                   layout:(UICollectionViewLayout *)collectionViewLayout
minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 10.0f;
}
#pragma mark -- 区头的高度
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    
    YFIntegralAreaHeaderView *testView= [[YFIntegralAreaHeaderView alloc] initWithFrame:CGRectMake(0, 0, UISCREEN_WIDTH, 0)];
    
    // 750 * 420  图片比例
    return CGSizeMake(UISCREEN_WIDTH, 200);
}


-(NSMutableArray *)goodsListArray{
    if (!_goodsListArray) {
        _goodsListArray = [NSMutableArray array];
    }
    return _goodsListArray;
}

- (NSMutableArray *)bannerArray {
    if (!_bannerArray) {
        _bannerArray = [NSMutableArray array];
    }
    return _bannerArray;
}
@end
